package cn.tedu.ems.controller;

import cn.tedu.ems.pojo.dto.NewsAddNewDTO;
import cn.tedu.ems.pojo.dto.NewsUpdateDTO;
import cn.tedu.ems.pojo.vo.NewsListItemVO;
import cn.tedu.ems.pojo.vo.NewsStandardVO;
import cn.tedu.ems.service.INewsService;
import cn.tedu.ems.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理新闻相关请求的控制器
 */
@Slf4j
@Api(tags = "07. 新闻管理模块")
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;

    public NewsController() {
        log.debug("创建控制器对象：NewsController");
    }

    @ApiOperation("添加新闻")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid NewsAddNewDTO newsAddNewDTO) {
        log.debug("开始处理【添加新闻】的请求，参数：{}", newsAddNewDTO);
        newsService.addNew(newsAddNewDTO);
        log.debug("添加新闻成功！");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除新闻")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除新闻失败，尝试删除的新闻的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除新闻】的请求，参数：{}", id);
        newsService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改新闻详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, NewsUpdateDTO newsUpdateDTO) {
        log.debug("开始处理【修改新闻详情】的请求，参数ID：{}, 新数据：{}", id, newsUpdateDTO);
        newsService.updateInfoById(id, newsUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询新闻详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<NewsStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询新闻详情】的请求，参数：{}", id);
        NewsStandardVO news = newsService.getStandardById(id);
        return JsonResult.ok(news);
    }

    @ApiOperation("根据标题查询新闻详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "title", value = "新闻title", required = true, dataType = "String")
    @GetMapping("/{title:[a-zA-Z0-9_\u4e00-\u9fa5]+}/title")
    public JsonResult<NewsStandardVO> getStandardByTitle(@PathVariable String title) {
        log.debug("开始处理【根据标题查询新闻详情】的请求，参数：{}", title);
        NewsStandardVO news = newsService.getStandardByTitle(title);
        return JsonResult.ok(news);
    }

    @ApiOperation("查询新闻列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<NewsListItemVO>> list() {
        log.debug("开始处理【查询新闻列表】的请求，无参数");
        List<NewsListItemVO> list = newsService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据标题查询新闻列表")
    @ApiOperationSupport(order = 430)
    @GetMapping("/{title:[a-zA-Z0-9_\u4e00-\u9fa5]+}/listByTitle")
    public JsonResult<List<NewsListItemVO>> listByTitle(@PathVariable String title) {
        log.debug("开始处理【查询新闻列表】的请求，无参数");
        List<NewsListItemVO> list = newsService.listByTitle(title);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据作者查询新闻列表")
    @ApiOperationSupport(order = 440)
    @GetMapping("/{author:[a-zA-Z_\u4e00-\u9fa5]+}/listByAuthor")
    public JsonResult<List<NewsListItemVO>> listByAuthor(@PathVariable String author) {
        log.debug("开始处理【查询新闻列表】的请求，无参数");
        List<NewsListItemVO> list = newsService.listByAuthor(author);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据部门查询新闻列表")
    @ApiOperationSupport(order = 450)
    @GetMapping("/{submissionDepartment:[a-zA-Z_\u4e00-\u9fa5]+}/listBySubmissionDepartment")
    public JsonResult<List<NewsListItemVO>> listBySubmissionDepartment(@PathVariable String submissionDepartment) {
        log.debug("开始处理【查询新闻列表】的请求，无参数");
        List<NewsListItemVO> list = newsService.listBySubmissionDepartment(submissionDepartment);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据栏目查询新闻列表")
    @ApiOperationSupport(order = 460)
    @GetMapping("/{submissionColumn:[\u4e00-\u9fa5]+}/listBySubmissionColumn")
    public JsonResult<List<NewsListItemVO>> listBySubmissionColumn(@PathVariable String submissionColumn) {
        log.debug("开始处理【查询新闻列表】的请求，无参数");
        List<NewsListItemVO> list = newsService.listBySubmissionColumn(submissionColumn);
        return JsonResult.ok(list);
    }
}